# -*- coding: UTF-8 -*-
import fcntl
import socket
import struct
import subprocess


def get_local_ip(ifname):
    """
    根据网口获取ip地址
    :param ifname:
    :return:
    """
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15]))
    ret = socket.inet_ntoa(inet[20:24])
    return ret


def get_device_info():
    with open('/root/device_info.txt', 'r') as f:
        device_info = f.read()
        return device_info.strip()
    return None


def is_reachable(ip):
    if ip.find(':') > 0:
        cmd = ["ping6", "-c", "2", ip]
    else:
        cmd = ["ping", "-c", "2", ip]
    if subprocess.call(cmd) == 0:
        return True
    else:
        return False


def is_valid_ip(ip_address):
    if not ip_address:
        return False
    parts = ip_address.split('.')

    # IP地址应该有四部分
    if len(parts) != 4:
        return False

    # 检查每个部分是否是0到255之间的整数
    for part in parts:
        try:
            num = int(part)
            if num < 0 or num > 255:
                return False
        except ValueError:
            return False

    return True


if __name__ == '__main__':
    print is_valid_ip('')
